package com.acegear.horizon.domain.models.jpa;

import com.fasterxml.jackson.annotation.JsonIgnore;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Created by wangsike on 2016/12/17.
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
public class SecureToken {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @Column(unique = true, columnDefinition = "VARCHAR(100)")
    private String token;

    @Column
    private Long expireAt;

    @Column
    private Boolean used;

    @Column
    private String phone;

    @Column
    private String email;

    @CreatedDate
    @Column
    private Long createAt;

    @LastModifiedDate
    @Column
    private Long updateAt;

    public SecureToken() {
        token = UUID.randomUUID().toString();
        expireAt = System.currentTimeMillis() + 3600 * 1000;
        used = false;
    }

    public static SecureToken secureTokenByPhone(String phone) {
        SecureToken secureToken = new SecureToken();
        secureToken.setPhone(phone);
        return secureToken;
    }

    public static SecureToken secureTokenByEmail(String email) {
        SecureToken secureToken = new SecureToken();
        secureToken.setEmail(email);
        return secureToken;
    }

    public Long getCreateAt() {
        return createAt;
    }

    public Long getUpdateAt() {
        return updateAt;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getExpireAt() {
        return expireAt;
    }

    public void setExpireAt(Long expireAt) {
        this.expireAt = expireAt;
    }

    public Boolean getUsed() {
        return used;
    }

    public void use() {
        this.used = true;
    }

    public Boolean verify(String str) {
        return getPhone() == null ? verifyEmail(str) : verifyPhone(str);
    }

    public Boolean verifyPhone(String str) {
       // if (!used && expireAt > System.currentTimeMillis() && str.equals(phone)) {
            use();
            return true;
//        } else {
//            return false;
//        }
    }

    public Boolean verifyEmail(String str) {
        if (!used && expireAt > System.currentTimeMillis() && str.equals(email)) {
            use();
            return true;
        } else {
            return false;
        }
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
